package top.callname.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;

import top.callname.bean.RspInfoBean;
import top.callname.bean.StudentBean;
import top.callname.service.StudentService;
import top.callname.service.impl.StudentServiceImpl;


@WebServlet("/admin/student")
public class StudentServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	private StudentService studentService = new StudentServiceImpl();
	private Logger logger = Logger.getLogger(StudentServlet.class);

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String method = request.getParameter("method");

		if("removeOpenid".equals(method)){
			//移除绑定的微信
			String sid = request.getParameter("sid");
			RspInfoBean rsp = studentService.solutionBinding(Integer.valueOf(sid));
			logger.debug("解除绑定："+rsp.toString());
			out.write(JSON.toJSONString(rsp));
		}else if("add".equals(method)){
			String cid = request.getParameter("cid");
			String stuNo = request.getParameter("stuNo");
			String stuName = request.getParameter("stuName");
			StudentBean student = new StudentBean();
			student.setCid(Integer.valueOf(cid));
			student.setStuNo(stuNo);
			student.setStuName(stuName);
			RspInfoBean rsp = studentService.save(student);
			logger.debug("添加学生："+rsp.toString());
			out.write(JSON.toJSONString(rsp));
		}else if("remove".equals(method)){
			String sid = request.getParameter("sid");
			RspInfoBean rsp = studentService.deleteBySid(Integer.valueOf(sid));
			logger.debug("删除学生："+rsp.toString());
			out.write(JSON.toJSONString(rsp));
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	

}
